package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.SolicitudPermiso;

public class SolicitudPermisoDAOImpl extends JdbcDaoSupport implements ISolicitudPermisoDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM ADM_SOLICITUD_PERMISO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(SolicitudPermiso solicitudPermiso) {
		deleteOperation.execute( solicitudPermiso  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public SolicitudPermiso findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(SolicitudPermiso solicitudPermiso) {
		insertOperation.execute( solicitudPermiso );
	}

	public void update(SolicitudPermiso solicitudPermiso) {
		updateOperation.execute( solicitudPermiso );
	}

	public SolicitudPermiso findByCriteria(SolicitudPermiso  solicitudPermiso) {
		return findByCriteriaQuery.runQuery(solicitudPermiso);
	}

	public List load(SolicitudPermiso  solicitudPermiso) {
		return findAllQuery.runQuery(solicitudPermiso);
	}

	public SolicitudPermiso findByActiveState(SolicitudPermiso  solicitudPermiso) {
		return findByActiveStateQuery.runQuery(solicitudPermiso);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
//    	deleteOperation = new DeleteOperation(  getDataSource() );
//    	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
//    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
//    	findAllQuery = new FindAllQuery( getDataSource() );
//    	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO ADM_SOLICITUD_PERMISO_T ("
				  + " ID, "
				  
				  + " ID_SOLICITUD, "
				  + " NUMERO_PERMISO, "
				  + " FECHA_INICIO, "
				  + " FECHA_FIN, "
				  + " ID_CONTRAVENCION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  
				  + " FECHA_CREA "
				  + ") VALUES ("
				  
			  	  + " ADM_SOLICITUD_PERMISO_SEC.nextVal, "
			  	  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(SolicitudPermiso solicitudPermiso) {
			InsertOperation.this.update( new Object[]{
				solicitudPermiso.getIdSolicitud(),
				solicitudPermiso.getNumeroPermiso(),
				solicitudPermiso.getFechaInicio(),
				solicitudPermiso.getFechaFin(),
				solicitudPermiso.getIdContravencion(),
				solicitudPermiso.getEstado(),
				solicitudPermiso.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE ADM_SOLICITUD_PERMISO_T SET "
					  + " ID_SOLICITUD = ? ,"
					  + " NUMERO_PERMISO = ? ,"
					  + " ID_CONTRAVENCION = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSTIMESTAMP "
				  	  + WHERE
    				  + " ID = ? ";
    	public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(SolicitudPermiso solicitudPermiso) {
			UpdateOperation.this.update( new Object[]{
				solicitudPermiso.getIdSolicitud(),
				solicitudPermiso.getNumeroPermiso(),
				solicitudPermiso.getIdContravencion(),
				solicitudPermiso.getEstado(),
				solicitudPermiso.getUsuarioModifica(),
				solicitudPermiso.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM ADM_SOLICITUD_PERMISO_T "
										+ WHERE;
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(SolicitudPermiso solicitudPermiso) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM ADM_SOLICITUD_PERMISO_T "
										+ WHERE + " 1=1 ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			SolicitudPermiso solicitudPermiso = new SolicitudPermiso();
				solicitudPermiso.setId(new Long(rs.getLong("ID")));
				solicitudPermiso.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
				solicitudPermiso.setNumeroPermiso(rs.getString("NUMERO_PERMISO"));
				solicitudPermiso.setFechaInicio(rs.getDate("FECHA_INICIO"));
				solicitudPermiso.setFechaFin(rs.getDate("FECHA_FIN"));
				solicitudPermiso.setIdContravencion(new Long(rs.getLong("ID_CONTRAVENCION")));
				solicitudPermiso.setEstado(rs.getString("ESTADO"));
				solicitudPermiso.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				solicitudPermiso.setFechaCrea(rs.getDate("FECHA_CREA"));		
				solicitudPermiso.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				solicitudPermiso.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return solicitudPermiso;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public SolicitudPermiso runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (SolicitudPermiso) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " AND ID_SOLICITUD = ? "
										+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public SolicitudPermiso runQuery(SolicitudPermiso solicitudPermiso) {
			List result =  execute( new Object[]{
					solicitudPermiso.getIdSolicitud(),
				});
			if ( result.size() > 0 ) {
        		return (SolicitudPermiso) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery(SolicitudPermiso solicitudPermiso) {
			List result =  execute( new Object[]{
					solicitudPermiso.getIdSolicitud()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
			  	  	  					+ " ESTADO = ? ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public SolicitudPermiso runQuery(SolicitudPermiso solicitudPermiso) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (SolicitudPermiso) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
